Skip to main content

Test Hooks

DevAssure test hooks are asynchronous functions that are injected before or after your tests . They are designed to be used for global setup and teardown logic that applies to all tests in your project.

DevAssure supports the following four hooks:

  • beforeEach: This hook is executed before each test. It's commonly used for tasks that need to be performed before every test, such as opening a new browser page, logging in to a web application, or navigating to a specific URL.

  • afterEach: This hook is executed after each test. It's typically used for tasks that need to be performed after every test, such as closing the browser page, logging out of a web application, or taking screenshots.

  • beforeAll: This hook is executed once before any of the tests start running. It's useful for one-time setup tasks that are required by all tests in a test suite, such as establishing a database connection.

  • afterAll: This hook is executed once after all the tests have been run. It's typically used for one-time teardown tasks that need to be performed after all tests have completed, such as closing any open database connections or cleaning up temporary files.

To create test hooks :

  • Select Custom Functions from the side navigation.
  • Click on Add new icon that appears on hover over the Hooks in explorer.
  • Select New Hooks and enter a name.
  • In the Test Types (web / api) , you can choose the type of test actions you need in the hooks. If your test hooks require performing UI actions along with API , such as clicking buttons or entering text, select "Web." If you need to perform only API related test actions, select "API." Both "Web" and "API" options are selected by default.

Example:

Icon

In this example ,

  • The beforeAll hook populates user data for all personas.
  • The beforeEach hook opens the login page of the web application and loads a pre-authenticated user session.
  • The afterEach hook saves the cookies to reuse the session using storage state.
  • Finally, the afterAll hook cleans up any resources created during the tests..

By using test hooks effectively, you can write cleaner, more maintainable, and more efficient tests in DevAssure.